home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6947 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  62 lines

  1. Newsgroups: comp.arch.arithmetic,comp.lang.c,comp.lang.c++
  2. Path: Utrecht.NL.net!news
  3. From: Franz Korntner <fkorntne@bazis.nl>
  4. Subject: Re: Access carry flag from C
  5. X-Nntp-Posting-Host: bastion1.bazis.nl
  6. Content-Type: text/plain; charset=us-ascii
  7. Message-ID: <312AFACE.41C6@bazis.nl>
  8. Sender: news@inter.NL.net (News at newsutr)
  9. Content-Transfer-Encoding: 7bit
  10. Organization: NLnet
  11. References: <Dn1C9z.DGv.0.net@indra.com> <1996Feb1922.17.19.879@koobera.math.uic.edu> <31298D20.41C6@bazis.nl> <danpop.824859220@rscernix>
  12. Mime-Version: 1.0
  13. Date: Wed, 21 Feb 1996 10:58:22 GMT
  14. X-Mailer: Mozilla 2.0b6a (X11; I; OSF1 V3.2 alpha)
  15.  
  16. Dan Pop wrote:
  17. > In <31298D20.41C6@bazis.nl> fkorntne@bazis.nl (Franz Korntner) writes:
  18. > >Steve Sullivan <sullivan@indra.com> wrote:
  19. > >>    i = j + k;
  20. > >>    if (overflowed) ....;
  21. > >
  22. > >Yes there is a method.
  23. > >
  24. > >j+k will overflow when the result exceeds MAXINT
  25. > >
  26. > >Thus:  "if (j+k > MAXINT) overflow();" but the operation is undefined
  27. > >if the result overflows, so the expression needs rewriting to make sure
  28. > >this doesn't happen. The result is then "if (j>MAXINT-k) overflow();".
  29. > If k is negative, MAXINT-k will overflow :-)
  30.  
  31. Later I pointed out that this was only true for unsigned numbers. Why
  32. should I start with the difficult case first?
  33.  
  34. > BTW, MAXINT is spelled INT_MAX in C.
  35.  
  36. I know that and I named it so deliberatly. Because we are testing overflow
  37. of arithmetic operations we must be aware of the size of the variables. In
  38. my example I didn't mention the type 'int', I just stated that I assumed
  39. 32 bit numbers. According to the (X3J11 / ANSI) standard, the constant
  40. INT_MAX (and in this case most limits in limits.h) denotes the minimal
  41. limit. It is possible that the physical limit is much higher (or lower 
  42. in the case of negative values). As I am only interested in the physical
  43. limit, the value INT_MAX has no meaning. Only the compiler is fully aware
  44. of these limits (and not a header file!) and what I really miss in the 
  45. standard and/or implementation is a inline function 'maxlimitof(int)',
  46. analogical to 'sizeof(int)'.
  47.  
  48. What will the future hold for 64 bit or larger machines. For the Alpha
  49. they declared int to be a 32 bit datatype because too many coders relied
  50. on this. Real pity as int should denote the natural size of the machine,
  51. and in the case of the Alpha it should have been 64! But this is a
  52. totally different discussion.
  53.  
  54. -- 
  55. +-----------------------------------------------------------------------+
  56. | Franz Korntner at BAZIS, dept. System Development, Leiden, Netherlands|
  57. | E-mail: fkorntne@hiscom.nl                                            |
  58. +-----------------------------------------------------------------------+
  59.